﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kangaroo.Configuration;
using Kangaroo.Model;

namespace Kangaroo.MapMarker
{
    public partial class MarkerForm : Form
    {
        private const int MarkerRadius = 15;
        private Image _map;
        private Config _config;
        private string _configFileName;

        public MarkerForm()
        {
            InitializeComponent();
            openFileDialog1.Filter = saveFileDialog1.Filter = "配置文件|*.cfg|所有文件|*.*";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _map = Image.FromFile("map.png");
            ReloadMap();
        }

        private void ReloadMap()
        {
            pictureBox1.Image = _map;
            pictureBox1.Refresh();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (_config == null)
            {
                return;
            }
            var channel = GetChannelInfo();
            if (channel != null)
            {
                var point = new Point(e.X, e.Y);
                DrawMarker(point, pictureBox1);
                var pointOnMap = ToPointOnMap(point,
                    new Point(pictureBox1.Width, pictureBox1.Height),
                    new Point(_map.Width, _map.Height));
                _config.ChannelPositions.Add(new Config.ChannelPosition
                {
                    Channel = channel,
                    X = pointOnMap.X,
                    Y = pointOnMap.Y
                });
                Text = string.Format("{0}, {1}", pointOnMap.X, pointOnMap.Y);
            }
        }

        private ChannelInfo GetChannelInfo()
        {
            var propertyForm = new PropertyForm(new ChannelInfo());
            if(propertyForm.ShowDialog() == DialogResult.OK)
            {
                return (ChannelInfo)propertyForm.Channel;
            }
            return null;
        }

        private static Point ToPointOnMap(
            Point pointOnControl, Point pictureSize, Point mapSize)
        {
            var x = (int)Math.Round(pointOnControl.X * 1.0 / pictureSize.X * mapSize.X);
            var y = (int)Math.Round(pointOnControl.Y * 1.0 / pictureSize.Y * mapSize.Y);
            return new Point(x, y);
        }

        private static Point FromPointOnMap(
            Point pointOnMap, Size pictureSize, Size mapSize)
        {
            var x = (int)Math.Round(pointOnMap.X * 1.0 / mapSize.Width * pictureSize.Width);
            var y = (int)Math.Round(pointOnMap.Y * 1.0 / mapSize.Height * pictureSize.Height);
            return new Point(x, y);
        }

        private static void DrawMarker(Point pointOnControl, PictureBox pictureBox)
        {
            var graphics = pictureBox.CreateGraphics();
            graphics.FillEllipse(new SolidBrush(Color.LimeGreen),
                (float)(pointOnControl.X - MarkerRadius / 2.0),
                (float)(pointOnControl.Y - MarkerRadius / 2.0),
                MarkerRadius, MarkerRadius);
        }

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            _config = new Config();
            ReloadMap();
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                LoadConfig();
            }
        }

        private void LoadConfig()
        {
            _config = Config.Load(_configFileName = openFileDialog1.FileName);
            ReloadMap();
            DrawMarkers(_config.ChannelPositions);
        }

        private void DrawMarkers(IEnumerable<Config.ChannelPosition> channelPositions)
        {
            foreach(var channelPosition in channelPositions)
            {
                DrawMarker(FromPointOnMap(new Point(channelPosition.X, channelPosition.Y),
                    pictureBox1.Size, _map.Size), pictureBox1);
            }
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            if(_configFileName == null)
            {
                if(saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    _configFileName = saveFileDialog1.FileName;
                }
            }
            _config.Save(_configFileName);
        }

        private void _configToolStripButton_Click(object sender, EventArgs e)
        {
            new PropertyForm(_config).ShowDialog();
        }
    }
}
